package com.example.service.impl;

import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.entity.dto.GongZhuangAddDto;
import com.example.entity.dto.GongZhuangSelectDto;
import com.example.entity.dto.GongZhuangUpdateDto;
import com.example.entity.mysql.Resource;
import com.example.entity.mysql.Resourcegroup;
import com.example.entity.vo.GongZhuangResultVo;
import com.example.entity.vo.GongZhuangVo;
import com.example.entity.yifei.Maconlineinfo;
import com.example.entity.yifei.Mocty;
import com.example.exception.CustomException;
import com.example.mapper.*;
import com.example.mapper.pc.ResourceMapper;
import com.example.mapper.pc.ResourcegroupMapper;
import com.example.mapper.pc.WorkresourceMapper;
import com.example.service.GongZhuangService;
import com.example.service.MaconlineinfoService;
import com.example.service.ResourcestatuslogService;
import com.example.service.pc.ResourceService;
import com.example.service.pc.ResourcegroupService;
import com.example.untils.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GongZhuangServiceImpl implements GongZhuangService {

    @Autowired
    private GongZhuangMapper mapper;

    @Autowired
    private ResourcegroupMapper groupMapper;

    @Autowired
    private MaconlineinfoService maconlineinfoService;
    @Autowired
    private MaconlineinfoMapper maconlineinfoMapper;
    @Autowired
    private MaconlineinfohistoryMapper maconlineinfohistoryMapper;
    @Autowired
    private ResourceService resourceService;
    @Autowired
    private MoctyMapper moctyMapper;
    @Autowired
    private ResourceMapper resourceMapper;
    @Autowired
    private WorkresourceMapper workresourceMapper;
    @Autowired
    private ResourcegroupService resourceGroupService;
    @Autowired
    private ResourceusehisMapper resourceusehisMapper;
    @Autowired
    private CmsmdMapper cmsmdMapper;
    @Autowired
    private ResourcestatuslogService resourcestatuslogService;

    @Override
    public GongZhuangResultVo list(GongZhuangSelectDto dto) {
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        dto.setDeviceId(StringUtils.isEmpty(dto.getDeviceId()) ? null:dto.getDeviceId());
        dto.setDeviceName(StringUtils.isEmpty(dto.getDeviceName()) ? null:dto.getDeviceName());
        dto.setStatus(StringUtils.isEmpty(dto.getStatus()) ? null:dto.getStatus());
        List<GongZhuangVo> list = mapper.list(dto);
        PageInfo<GongZhuangVo> pageInfo = new PageInfo<>(list);
        for(GongZhuangVo vo : pageInfo.getList()) {  // 查询工装对应的机器是哪个，机器所生产的工单又是哪个
            // 去macOnlineInfo上找，对应的机器号
            Maconlineinfo maconlineinfo = maconlineinfoMapper.selectOne(new LambdaQueryWrapper<Maconlineinfo>().eq(Maconlineinfo::getResourceno, vo.getDeviceId()));
            if(maconlineinfo != null) {
                vo.setMacNo(maconlineinfo.getMacno());
                if(StringUtils.isEmpty(maconlineinfo.getMacno())) continue;
                Resource resource = resourceMapper.selectOne(new LambdaQueryWrapper<Resource>().eq(Resource::getDeviceid, maconlineinfo.getMacno()));
                if(resource == null) continue;
                // SELECT * FROM MOCTY WHERE UDF05 = 'AMM2AMM2024-011948-CT-SBD-19-004-D01'
                // SELECT * FROM MOCTY WHERE UDF02 = 'a4e6c9ab-8395-4be7-a494-88310d595f7c'
                if(StringUtils.isEmpty(resource.getMoctyudf05())) continue;
                Mocty one = moctyMapper.selectOne(new LambdaQueryWrapper<Mocty>().eq(Mocty::getUdf05, resource.getMoctyudf05()));
                if(one == null) continue;
                if(StringUtils.isNotEmpty(one.getUdf02())) {
                    List<Mocty> moctyList = moctyMapper.selectList(new LambdaQueryWrapper<Mocty>().eq(Mocty::getUdf02, one.getUdf02()));
                    StringBuffer sb = new StringBuffer();
                    for(Mocty x : moctyList) {
                        sb.append(x.getTy001()+"-"+x.getTy002()+",");
                    }
                    if(sb.length() > 0) sb.deleteCharAt(sb.length() - 1);
                    vo.setDoingWork(sb.toString());
                }else{
                    vo.setDoingWork(one.getTy001()+"-"+one.getTy002());
                }
            }
        }

        return new GongZhuangResultVo((int) pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    @DSTransactional
    public void update(GongZhuangUpdateDto dto) {
        if (dto.getResourceGroupIdIsF()!=null && !dto.getResourceGroupIdIsF().isEmpty()){
            GongZhuangVo entity = mapper.selectById(dto.getResourceId());
            dto.setDeviceId(dto.getDeviceId()==null ? entity.getDeviceId() : dto.getDeviceId());
            dto.setDeviceName(dto.getDeviceName()==null ? entity.getDeviceName() : dto.getDeviceName());
            if (!entity.getDeviceId().equals(dto.getDeviceId()) && mapper.noNumber(dto.getDeviceId()).compareTo(1L)>=0)
                throw new CustomException("500","资源组编号已存在");
            mapper.updateGroup(dto.getResourceGroupIdIsF(),dto.getDeviceId(),dto.getDeviceName());
        }
        GongZhuangVo gongZhuangVo = new GongZhuangVo();
        BeanUtils.copyProperties(dto,gongZhuangVo);
        mapper.updateById(gongZhuangVo);
    }

    @Override
    @DSTransactional
    public void add(GongZhuangAddDto dto) {
        if (StringUtils.isEmpty(dto.getDeviceId())) return;
        if (mapper.noNumber(dto.getDeviceId()).compareTo(1L)>=0)
            throw new CustomException("500","资源组编号已存在");
        else if (StringUtils.isEmpty(dto.getResourceGroupId()))
            throw new CustomException("500","父级资源组必须存在");
        //开始存储resourceGroup表
        Resourcegroup groupEntity = new Resourcegroup()
                .setResourceGroupNO(dto.getDeviceId()).setResourceGroupName(dto.getDeviceName())
                .setResourceGroupParentId(Integer.valueOf(dto.getResourceGroupId()))
                .setType("F");
        groupMapper.insert(groupEntity);
        //开始存储resource表
        GongZhuangVo gongZhuangVo=new GongZhuangVo().setResourceGroupId(dto.getResourceGroupId())
                .setDeviceId(dto.getDeviceId()).setDeviceName(dto.getDeviceName())
                .setResourceGroupIdIsF(groupEntity.getResourceGroupId().toString())
                .setResourceType("4").setDeviceStatus("启用").setStatus("闲置");
        mapper.insert(gongZhuangVo);

//        resourcestatuslogService.save();
    }
}
